//
//  HTTabBar.swift
//  weibo
//
//  Created by JonsonLiu on 16/4/16.
//  Copyright © 2016年 liujonson. All rights reserved.
//

import UIKit

class HTTabBar: UITabBar {

    
    var compoButtomClick:(() -> ())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(compoButton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        compoButton.center = CGPoint(x: self.frame.width*0.5, y: self.frame.height*0.5)
        
        let itemH = self.frame.width / 5
        
        var index = 0
        
        for value in subviews{
        
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                value.frame.size.width = itemH
                
                value.frame.origin.x = CGFloat(index) * itemH
                
                index++
                
                if index == 2{
                
                    index++
                }
            
            }
        }
        
    }
    
    private lazy var compoButton: UIButton = {
    
        let button = UIButton()
        
        //监听事件
        button.addTarget(self, action: "compoButtonClickButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        
//        button.setBackgroundImag(UIImage(named: "tabbar_compose_button"), forState: .Normal)
//        
//        button.setBackgroundImag(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        button.sizeToFit()
        
        return button
    
    }()
    
    
    
    
    func compoButtonClickButton(){
    
        compoButtomClick?()
    }
    
}
